// @flow
import React, { Component } from 'react'
import { AgGridReact } from 'ag-grid-react'
import styles from './ResumeGrid.css'
import { TableResize } from 'ag-grid-addons'
import moment from 'moment'
import { connect } from 'react-redux'
import LinkToDeviceOverView from './LinkToDeviceOverView'
// import rowData from './rowData'

type Props = {
  history: Object,
  rowData: Array<Object>,
  deviceId: number
}
type States = {
  columnDefs: Array<Object>
}
export class ResumeGrid extends Component {
  props: Props
  state: States
  gridOptions: Object
  api: Object
  onGridReady: () => void
  TableResize: () => void
  goToDeviceOverView: Function
  constructor (props: Props) {
    super(props)
    const component = this
    this.state = {
      columnDefs: [
        {
          headerName: '事件名称',
          field: 'eventName',
          suppressMenu: true,
          cellRendererParams: {
            goToDeviceOverView: component.goToDeviceOverView.bind(this)
          },
          cellRendererFramework: LinkToDeviceOverView,
          cellStyle: {
            'padding': '8px'
          }
        }, {
          headerName: '客户',
          field: 'customerId',
          cellStyle: {
            padding: '8px'
          },
          cellRenderer: (params) => {
            if (params.data.customer != null) {
              return params.data.customer.name
            }
            return ''
          }
        }, {
          headerName: '设备',
          field: 'deviceId',
          cellStyle: {
            padding: '8px'
          },
          cellRenderer: (params) => {
            if (params.data.device != null) {
              return params.data.device.name
            }
            return ''
          }
        }, {
          headerName: '部件',
          field: 'components',
          cellStyle: {
            padding: '8px'
          },
          cellRenderer: (params) => {
            if (params.data.components != null) {
              let com1 = params.data.components.split('%%').map((d, i) => {
                return '<span>' + d + '</span>'
              })
              return com1
            }
            return ''
          }
        }, {
          headerName: '事件类型',
          field: 'eventType',
          cellStyle: {
            padding: '8px'
          }
        }, {
          headerName: '开始时间',
          field: 'startTime',
          cellStyle: {
            padding: '8px'
          },
          cellRenderer: (params) => {
            return moment(params.data.startTime, 'YYYY-MM-DDTHH:mm:ss.SSSZ').format('YYYY-MM-DD HH:mm:ss')
          }
        }, {
          headerName: '结束时间',
          field: 'endTime',
          cellStyle: {
            padding: '8px'
          },
          cellRenderer: (params) => {
            return moment(params.data.endTime, 'YYYY-MM-DDTHH:mm:ss.SSSZ').format('YYYY-MM-DD HH:mm:ss')
          }
        },
        {
          headerName: '负责人',
          field: 'owner',
          cellStyle: {
            padding: '8px'
          }
        },
        {
          headerName: '状态',
          field: 'eventStatus',
          cellStyle: {
            padding: '8px'
          }
        }
      ]
    }
    this.gridOptions = {
      localeText: {
        loadingOoo: '数据加载中...',
        noRowsToShow: '暂无数据',
        filterOoo: '搜索当前页面',
        equals: '等于',
        notEqual: '不等于',
        contains: '包含',
        startsWith: '开始于',
        endsWith: '结束于',
        copy: '复制',
        paste: '粘贴',
        copyWithHeaders: '带表头复制',
        toolPanel: '工具栏',
        export: '导出',
        csvExport: '导出CSV',
        excelExport: '导出Excel'
      },
      enableSorting: true,
      enableFilter: true,
      rowSelection: 'single',
      rowDeselection: true,
      enableColResize: true
    }
    this.onGridReady = this.onGridReady.bind(this)
    this.TableResize = TableResize.bind(this)
    this.goToDeviceOverView = this.goToDeviceOverView.bind(this)
  }
  goToDeviceOverView (data: Object) {
    let channel = []
    if (data.checkName) {
      channel = [data.channelId + '__' + data.checkName.split('__')[1]]
    }
    const { deviceId, history } = this.props
    const location = {
      pathname:`/device-file/${deviceId}/condition`,
      state: {
        defaultActiveKey: 'historyTab',
        // dynamicIndicator: '', // 以前不传指标的时候
        dynamicIndicator: data.sensorNameEn + '___' + data.variableNameEn,
        defaultChannel: channel,
        startTime: data.startTime,
        endTime: data.endTime
      }
    }
    history.push(location)
  }
  onGridReady (params: Object) {
    this.api = params.api
    this.api.sizeColumnsToFit()
    this.TableResize()
  }

  render () {
    const { rowData } = this.props
    const { columnDefs } = this.state
    return (
      <div className={styles['grid']}>
        <div className={styles['content'] + ' ag-fresh'}>
          <AgGridReact
            gridOptions={this.gridOptions}
            rowHeight='34'
            headerHeight='28'
            columnDefs={columnDefs}
            rowData={rowData}
            onGridReady={this.onGridReady}
          />
        </div>
      </div>
    )
  }
}
const mapStatesToProps = (state) => {
  return {
    deviceId: state.deviceCard.deviceData.id
  }
}
export default connect(mapStatesToProps, {})(ResumeGrid)
